﻿using RayDen.Library.Core;
using RayDen.Library.Core.Primitives;
using RayDen.RayEngine.Core.Types;
using RayEngine.Spectral.Entities.Materials;

namespace RayEngine.Spectral.Entities.Lighting
{
    public abstract class BaseInfiniteLight : BaseLight
    {
        public BaseInfiniteLight(LightMaterial mt)
            : base(mt)
        {
        }

        public override bool IsDelta()
        {
            return true;
        }

        public override IColorType Emit(ref Vector dir, out float pdf)
        {
            pdf = dir.z * MathLab.INVPI;
            return Le(ref dir);
        }

        public override void EvaluateShadow(ShadePointInfo pt, float u0, float u1, float u2, out IColorType radiance, out float pdf, out RayData ray)
        {
            var wi = MC.CosineSampleHemisphere(u1, u2);
            pdf = wi.z * MathLab.INVPI;
            wi = pt.Frame.ToWorld(ref wi);
            ray = new RayData(ref pt.HitPoint, ref wi, 1e-4f, float.MaxValue);
            radiance = Le(ref wi);
        }

        public abstract IColorType Le(ref Vector dir);
    }
}
